package com.project.ryudo.SessionBean;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.project.entity.Account;
import com.project.entity.Property_Image;

/**
 * Session Bean implementation class MStatBean
 */
@Stateless
@LocalBean
public class MStatBean implements MStatBeanRemote {

    /**
     * Default constructor. 
     */
	@PersistenceContext
 	EntityManager em;
    public MStatBean() {
        // TODO Auto-generated constructor stub
    }
	@Override
	public List latestProperty() {
		Query query = em.createQuery("select prop from Property prop where prop.status = 1 order by prop.postDate desc");
		List result = query.setMaxResults(5).getResultList();
		return result;
	}
	@Override
	public List latestPropertyImages(String pid) {
		Query query = em.createQuery("select imgs from Property_Image imgs where imgs.property.propID = :propID");
		query.setParameter("propID", pid);
		List result = query.setMaxResults(1).getResultList();
		return result;
	}
	@Override
	public List recommendProperty(String status,Account mAcc) 
	{
		List result = null;
		if(status.equalsIgnoreCase("most_view"))
		{
			Query query = em.createQuery("SELECT prop FROM Property prop LEFT JOIN prop.visitLogs vlog WHERE prop.status = 1 ORDER BY vlog.count DESC");
			result = query.setMaxResults(5).getResultList();
			return result;
		}
		else if(status.equalsIgnoreCase("new_user"))
		{
			Query query = em.createQuery("SELECT prop FROM Property prop WHERE prop.state = :accState AND prop.status = 1");
			query.setParameter("accState", mAcc.getState());
			result = query.setMaxResults(5).getResultList();
			return result;
		} else if(status.equalsIgnoreCase("most_view_by_user"))
		{
			Query query = em.createQuery("SELECT prop FROM Property prop WHERE prop.state IN (SELECT prop2.state FROM Property prop2 WHERE prop2.propID IN" +
					"(SELECT vlog.property.propID FROM VisitLog vlog WHERE vlog.account.accID = :accID)) AND prop.status = 1");
			query.setParameter("accID", mAcc.getAccID());
			result = query.setMaxResults(5).getResultList();
			return result;
		}
		return result;
	}
	@Override
	public List recommendPropertyImages(String pid) {
		Query query = em.createQuery("select imgs from Property_Image imgs where imgs.property.propID = :propID");
		query.setParameter("propID", pid);
		List result = query.setMaxResults(1).getResultList();
		return result;
	}
	
	@Override
	public List vlogbyuser(int accID) {
		Query query = em.createQuery("SELECT vlog from VisitLog vlog where vlog.account.accID = :accID");
        query.setParameter("accID", accID);
		List result = query.setMaxResults(1).getResultList();
		return result;
	}
	@Override
	public List propertyByUser(int accID) {
		Query query = em.createQuery("select prop from Property prop where prop.status = 1 AND prop.account.accID = :accID order by prop.postDate desc");
		query.setParameter("accID", accID);
		List result = query.getResultList();
		return result;
	}
	@Override
	public List propertyImageByUser(String pid) {
		Query query = em.createQuery("select imgs from Property_Image imgs where imgs.property.propID = :propID");
		query.setParameter("propID", pid);
		List result = query.setMaxResults(1).getResultList();
		return result;
	}
	@Override
	public int reportEachPropertyViews(String type)
	{
		String propType = null;
		int TotalCount = 0;
		if(type.equalsIgnoreCase("PropForRent"))
		{
			propType = "P-";
		} else if(type.equalsIgnoreCase("PropForSale"))
		{
			propType = "S-";
		} else if(type.equalsIgnoreCase("RoomForRent"))
		{
			propType = "R-";
		}
		Query query = em.createQuery("select vlog.count from VisitLog vlog where vlog.property.propID LIKE '%"+propType+"%'");

		
		List result = query.getResultList();
		Iterator ite = result.iterator();
		while(ite.hasNext())
		{
			int re = Integer.parseInt(String.valueOf(ite.next()));
			TotalCount += re;
		}
		return TotalCount;
		
	}
	@Override
	public int reportEachPropertyTotal(String type) {
		String propType = null;
		int TotalCount = 0;
		if(type.equalsIgnoreCase("PropForRent"))
		{
			propType = "P-";
		} else if(type.equalsIgnoreCase("PropForSale"))
		{
			propType = "S-";
		} else if(type.equalsIgnoreCase("RoomForRent"))
		{
			propType = "R-";
		}
		Query query = em.createQuery("select prop from Property prop where prop.propID LIKE '%"+propType+"%'");

		
		List result = query.getResultList();
		
		return result.size();
		
	}
	@Override
	public int totalUsers() {
		Query query = em.createQuery("select acc from Account acc");
		List result = query.getResultList();
		return result.size();
	}
	@Override
	public int totalViews() {
		int TotalCount = 0;
		Query query = em.createQuery("select vlog.count from VisitLog vlog");
		List result = query.getResultList();
		Iterator ite = result.iterator();
		while(ite.hasNext())
		{
			int re = Integer.parseInt(String.valueOf(ite.next()));
			TotalCount += re;
		}
		return TotalCount;
	}
	@Override
	public int totalProps() {
		Query query = em.createQuery("select prop from Property prop");
		List result = query.getResultList();
		return result.size();
	}
	@Override
	public int totalStaff() {
		Query query = em.createQuery("select stf from Staff stf");
		List result = query.getResultList();
		return result.size();
	}

	
	
	

}
